package com.itheima.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.Dept;
import com.itheima.domain.system.Module;
import com.itheima.domain.system.Role;
import com.itheima.domain.system.User;
import com.itheima.service.system.DeptService;
import com.itheima.service.system.RoleService;
import com.itheima.service.system.UserService;
import com.itheima.util.MailUtil;
import com.itheima.web.controller.BaseController;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.*;

@Controller
@RequestMapping(value = "/system/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private DeptService deptService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private AmqpTemplate amqpTemplate;

    @RequestMapping(value = "/list", name = "分页查询用户列表")
    @RequiresPermissions("用户管理")
    public String findAll(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer size) {
        PageInfo<User> pageInfo = userService.findByPage(page, size, companyId);
        request.setAttribute("page", pageInfo);
        return "system/user/user-list";
    }

    @RequestMapping(value = "/toUpdate", name = "跳转到用户修改页面")
    @RequiresPermissions("修改用户")
    public String toUpdate(String id) {
        User user = userService.findById(id);
        List<Dept> list = deptService.findAll(companyId);
        request.setAttribute("deptList", list);
        request.setAttribute("user", user);
        return "/system/user/user-update";
    }

    @RequestMapping(value = "/toAdd", name = "跳转到用户增加页面")
    @RequiresPermissions("新增用户")
    public String toAdd() {
        List<Dept> list = deptService.findAll(companyId);
        request.setAttribute("deptList", list);
        return "/system/user/user-add";
    }

    @RequestMapping(value = "/edit", name = "编辑用户信息")
    public String edit(User user) throws Exception {
        user.setCompanyId(companyId);
        user.setCompanyName(companyName);
        if (StringUtils.isEmpty(user.getId())) {
            user.setId(UUID.randomUUID().toString());
            String password = user.getPassword();
            userService.save(user);

            //新增用户时，需要给该用户发送一封邮件
            String to = user.getEmail();
            String subject = "欢迎来到SaaS-Export大家庭";
            String content = "您使用的SaaS-Export平台通过：http://localhost:8080 进行登录，用户名为：" + to + "， 密码是：" + password;
            //MailUtil.sendMsg(to, subject, content);

            /**
             * 将邮件的内容写入RabbitMQ
             *  1、注入一个AmqpTemplate
             *  2、调用amqpTemplate.convertAndSend
             */

            Map map = new HashMap();
            map.put("to", to);
            map.put("subject", subject);
            map.put("content", content);

            //将邮件的内容发送消息
            //两个参数：
            //1、队列名称
            //2、消息内容
            amqpTemplate.convertAndSend("mail.send", map);
        } else {
            userService.update(user);
        }
        return "redirect:/system/user/list.do";
    }

    @RequestMapping(value = "/delete", name = "删除用户信息")
    @RequiresPermissions("删除用户")
    public String delete(String id) {
        userService.delete(id);
        return "redirect:/system/user/list.do";
    }

    @RequestMapping(value = "/roleList", name = "跳转用户分配角色页面")
    public String roleList(String id) {
        User user = userService.findById(id);
        request.setAttribute("user", user);

        List<Role> roleList = roleService.findAll(companyId);
        request.setAttribute("roleList", roleList);

        List<Role> userRole = roleService.findByUserId(id);

        String userRoleStr = "";
        for (Role role : userRole) {
            userRoleStr += role.getId() + ",";
        }
        request.setAttribute("userRoleStr", userRoleStr);

        return "/system/user/user-role";
    }

    @RequestMapping(value = "/changeRole", name = "保存用户角色")
    public String changeRole(String userid, String[] roleIds) {
        roleService.changeRole(userid, roleIds);
        return "redirect: /system/user/list.do";
    }
}
